const BASE_URL = "http://119.91.61.208:4399";

function request(params) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.responseType = params.type || "json";
    xhr.open(params.method || "GET", BASE_URL + params.url);
    xhr.setRequestHeader(params.header || "Content-Type", "application/x-www-form-urlencoded");
    xhr.send();
    xhr.onreadystatechange = () => {
      if (xhr.readyState === 4) {
        if (xhr.status >= 200 && xhr.status < 300) {
          if (xhr.response.msg == "success") resolve(xhr.response);
        } else {
          reject(new Error(xhr.response + "error"));
          alert("请求失败");
        }
      }
    };
  });
}
